# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\http_clients\status_api\__init__.py
__author__ = "Attila Gerendi (Sanyi)"
import logging, requests, requests.exceptions
from api.classes.status import ClsVulnerability, ClsMessage
from helpers.http_clients.exceptions import *
from settings_provider import settings

_logger = logging.getLogger("helpers.http_clients.status_api")
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class ScanStatusClient:
    def __init__(
        self,
        endpoint,
        api_key=None,
        headers=None,
        ssl_verify=False,
        request_timeout=None,
    ):
        self.endpoint = endpoint
        self.api_key = api_key
        self.ssl_verify = ssl_verify
        self.session = requests.session()
        if headers is None:
            self.headers = dict()
        if api_key:
            self.headers["X-Auth"] = api_key
        self.request_timeout = request_timeout
        if ssl_verify is False:
            self.ssl_verify = False
        else:
            if ssl_verify is True:
                ssl_ca_bundle_path = settings.get("ssl_ca_bundle_path", None)
                if ssl_ca_bundle_path:
                    self.ssl_verify = ssl_ca_bundle_path
                else:
                    self.ssl_verify = None

    def _do_request(self, method, url, headers=None, **kwargs):
        if headers is None:
            headers = self.headers
        else:
            headers.update(self.headers)
        if self.request_timeout is not None:
            kwargs["timeout"] = self.request_timeout
        try:
            result = method(url=url, verify=self.ssl_verify, headers=headers, **kwargs)
        except Exception as e:
            raise RequestError(str(e))

        if result.status_code == 204:
            return
        if result.status_code == 200:
            try:
                result = result.json()
            except Exception:
                raise RequestParsingError()

            return result
        if result.status_code == 201:
            try:
                result = result.json()
            except Exception:
                raise RequestParsingError()

            return result
        if result.status_code == 401:
            raise AuthException()
        else:
            if result.status_code == 404:
                raise HTTP404()
            else:
                if result.status_code == 400:
                    try:
                        result = result.json()
                    except Exception:
                        raise RequestParsingError()

                    raise HTTP400(result=result)
                else:
                    if result.status_code == 409:
                        try:
                            result = result.json()
                        except Exception:
                            raise RequestParsingError()

                        raise HTTP409(result=result)
                    else:
                        if result.status_code == 500:
                            try:
                                result = result.json()
                            except Exception:
                                raise RequestParsingError()

                            raise HTTP500(result=result)
                        else:
                            _logger.error(
                                "unhandled http result type: %s", result.status_code
                            )
                            raise RequestParsingError(str(result))

    def get_request(self, path, headers=None):
        return self._do_request(
            self.session.get, url=self.endpoint + path, headers=headers
        )

    def delete_request(self, path, headers=None):
        return self._do_request(
            self.session.delete, url=self.endpoint + path, headers=headers
        )

    def post_request(self, path, data, headers=None):
        return self._do_request(
            self.session.post, url=self.endpoint + path, headers=headers, json=data
        )

    def patch_request(self, path, data, headers=None):
        return self._do_request(
            self.session.patch, url=self.endpoint + path, headers=headers, json=data
        )

    def add_app_message(self, message):
        self._do_request(
            self.session.post, self.endpoint + "/message", json=message.to_dict()
        )

    def add_vulnerability(self, vulnerability):
        self._do_request(
            self.session.post,
            url=self.endpoint + "/vulnerabilities",
            json=vulnerability.to_dict(),
        )

    def update_app_status(self, app_status):
        self._do_request(
            self.session.patch,
            url=self.endpoint + "/app_status",
            json=app_status.to_dict(),
        )

    def update_scan_info(self, scan_info):
        self._do_request(
            self.session.patch,
            url=self.endpoint + "/scan_info",
            json=scan_info.to_dict(),
        )

    def add_location(self, location):
        self._do_request(
            self.session.post, url=self.endpoint + "/locations", json=location.to_dict()
        )

    def update_location(self, location):
        self._do_request(
            self.session.patch,
            url=self.endpoint + "/locations",
            json=location.to_dict(),
        )

    def add_location_tags(self, loc_id, tags):
        self._do_request(
            self.session.post,
            url=self.endpoint + "/locations/%s/tags" % (loc_id,),
            json=tags,
        )

    def add_external_urls(self, external_urls):
        self._do_request(
            self.session.post, url=self.endpoint + "/external_urls", json=external_urls
        )

    def register_worker(self, scanning_app, endpoint):
        return self._do_request(
            self.session.post,
            url=self.endpoint + "/workers/register",
            json=dict(scanning_app=scanning_app, endpoint=endpoint),
        )
